package com.study.leetcode;

/**
 * @description: SortingList  排序链表
 * @date: 2020/11/21 19:25
 * @author: hcm
 * @version: 1.0
 */
public class SortingList {
    private static ListNode head = null; //头节点
    public static void main(String[] args) {
        add(3);
        add(1);
        add(2);
        add(4);
        System.out.println(head);
        sortList(head);
    }

    private static void add(int i) {

        ListNode newNode = new ListNode(i);//实例化一个新节点a
        if (head == null) { //若头节点为空，新节点赋值给头节点
            head = newNode;
        }
        ListNode cur = head;  //若头节点不为空，用cur代替head进行操作。防止修改head的值
        while (cur.next != null) { //遍历到最后一个节点
            cur = cur.next;
        }
        cur.next = newNode ; //让
    }

    /**
     * 排序链表
     * @param head
     * @return
     */
    public static ListNode sortList(ListNode head) {

        //打印链表
        System.out.println(head.val);

        return head;
    }

}
class ListNode{
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode() {
    }
}